home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_06_07 / v6n7071a.txt < prev    next >
Text File  |  1989-09-26  |  4KB  |  167 lines

  1. /* RAM disk */
  2. /* Philip J. Erdelsky - June 23, 1988 */
  3.  
  4. #include "dd.h"
  5.  
  6. #define SECTOR_SIZE            512
  7. #define KILOBYTE              1024
  8. #define DIRECTORY_ENTRY_SIZE    32
  9. #define TBA                      0
  10.  
  11. #define DIRECTORY_ENTRIES_PER_SECTOR (SECTOR_SIZE/DIRECTORY_ENTRY_SIZE)
  12.  
  13. struct sector {
  14.   bite x[SECTOR_SIZE];
  15.   };
  16.  
  17. static struct sector *buffer;
  18.  
  19. /* bootstap block */
  20.  
  21. static bite jump[3] = {0,0,0};
  22. static char OEM[8] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
  23.  
  24. /* BIOS Parameter Block */
  25.  
  26. struct bpb BPB = {
  27.   SECTOR_SIZE,  /* bytes_per_sector            */
  28.   1,            /* sectors_per_allocation_unit */
  29.   1,            /* reserved_sectors            */
  30.   1,            /* FATs                        */
  31.   TBA,          /* root_directory_entries      */
  32.   TBA,          /* total_sectors               */
  33.   0xFE,         /* media_descriptor            */
  34.   TBA,          /* sectors_per_FAT             */
  35.   8,            /* sectors_per_track           */
  36.   1,            /* heads                       */
  37.   0             /* hidden_sectors              */
  38.   };
  39.  
  40. static struct bpb *bpb_table[1];
  41.  
  42. static unsigned int get_number(p) char *p; {
  43. unsigned int n = 0;
  44. int c;
  45. if (p==NULL) return 0;
  46. c = *p++;
  47. while ('0'<=c && c<='9') {
  48.   n = 10*n + c - '0';
  49.   c = *p++;
  50.   }
  51. return n;
  52. }
  53.  
  54. void init() {
  55. unsigned int capacity;
  56. capacity = get_number(argument(1));
  57.   if (capacity==0) capacity = 160;
  58. BPB.root_directory_entries = get_number(argument(2));
  59. if (BPB.root_directory_entries==0) BPB.root_directory_entries = 64;
  60. BPB.root_directory_entries =
  61.   (BPB.root_directory_entries + (DIRECTORY_ENTRIES_PER_SECTOR-1))
  62.     & (-DIRECTORY_ENTRIES_PER_SECTOR);
  63. BPB.total_sectors = capacity*(KILOBYTE/SECTOR_SIZE);
  64.  
  65.   {
  66.   unsigned int FAT_bytes;
  67.   FAT_bytes = BPB.total_sectors + (BPB.total_sectors+1)/2;
  68.   BPB.sectors_per_FAT =
  69.     (FAT_bytes + SECTOR_SIZE - 1) / SECTOR_SIZE;
  70.   }
  71.  
  72. puts("\nRAM disk installed, capacity = ");
  73. puts(capacity==160 ? "160" : argument(1));
  74. puts("K\n");
  75.  
  76. buffer = (struct sector *) (request_header->x.init.end_of_driver);
  77. request_header->x.init.end_of_driver += 
  78.   (long) BPB.total_sectors * (long) BPB.bytes_per_sector;
  79.  
  80. buffer[0] = *((struct sector *)(&jump));
  81.  
  82.   {
  83.   int i;
  84.   char *q;
  85.   q = buffer[BPB.reserved_sectors].x;
  86.   for (i=0;
  87.        i<BPB.sectors_per_FAT*BPB.FATs+
  88.                     DIRECTORY_ENTRY_SIZE*BPB.root_directory_entries;
  89.        i++)
  90.     q[i] = 0;
  91.   }
  92.  
  93. buffer[BPB.reserved_sectors].x[0] = BPB.media_descriptor;
  94. buffer[BPB.reserved_sectors].x[1] = 0xFF;
  95. buffer[BPB.reserved_sectors].x[2] = 0xFF;
  96.  
  97. bpb_table[0] = &BPB;
  98. request_header->x.init.pointer.bpb_table = bpb_table;
  99. request_header->x.init.number_of_units = 1;
  100.  
  101. request_header->status = DONE_STATUS;
  102. }
  103.  
  104. void media_check() {
  105. request_header->x.media_check.returned = 1;
  106. request_header->status = DONE_STATUS;
  107. }
  108.  
  109. void build_bpb() {
  110. request_header->x.build_bpb.bpb = &BPB;
  111. request_header->status = DONE_STATUS;
  112. }
  113.  
  114. void ioctl_input() {bad_device_driver_function();}
  115.  
  116. void input() {
  117. unsigned sector, n;
  118. struct sector *p;
  119. sector = request_header->x.io.starting_sector;
  120. p = (struct sector *) (request_header->x.io.transfer_address);
  121. n = request_header->x.io.count;
  122. while (n>0 && sector<BPB.total_sectors) {
  123.   *p++ = buffer[sector++];
  124.   n--;
  125.   }
  126. request_header->status = DONE_STATUS;
  127. }
  128.  
  129. void nondestructive_input() {
  130. request_header->status = DONE_STATUS;
  131. }
  132.  
  133. void input_status() {
  134. request_header->status = DONE_STATUS;
  135. }
  136.  
  137. void input_flush() {
  138. request_header->status = DONE_STATUS;
  139. }
  140.  
  141. void output() {
  142. unsigned sector, n;
  143. struct sector *p;
  144. sector = request_header->x.io.starting_sector;
  145. p = (struct sector *) (request_header->x.io.transfer_address);
  146. n = request_header->x.io.count;
  147. while (n>0 && sector<BPB.total_sectors) {
  148.   buffer[sector++] = *p++;
  149.   n--;
  150.   }
  151. request_header->status = DONE_STATUS;
  152. }
  153.  
  154. void output_with_verify() {
  155. output();
  156. }
  157.  
  158. void output_status() {
  159. request_header->status = DONE_STATUS;
  160. }
  161.  
  162. void output_flush() {
  163. request_header->status = DONE_STATUS;
  164. }
  165.  
  166. void ioctl_output() {bad_device_driver_function();}
  167.